//
//  BatteyPlugin.swift
//  Runner
//
//  Created by longxiaobai on 2024/8/8.
//

import Foundation
import Flutter

class BatteyPlugin: NSObject, FlutterPlugin {
    public static func register(with registrar: FlutterPluginRegistrar) {
        let channel = FlutterMethodChannel(
            name: "samples.flutter.dev/battery",
            binaryMessenger: registrar.messenger())
        let instance = BatteyPlugin()
        registrar.addMethodCallDelegate(instance, channel: channel)
    }
    
    public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
        switch call.method {
        case "getBatteryLevel":
            getBatteryLevel(result: result)
        default:
            result(FlutterMethodNotImplemented)
        }
    }
    
    private func getBatteryLevel(result: @escaping FlutterResult) {
        // 实现获取电池电量的逻辑
        let device = UIDevice.current
        device.isBatteryMonitoringEnabled = true
        if device.batteryState == UIDevice.BatteryState.unknown {
            result(FlutterError(code: "UNAVAILABLE",
                                message: "Battery level not available.",
                                details: nil))
        } else {
            result(Int(device.batteryLevel * 100))
        }
    }
}
